package com.gitee.wsl.api

import kotlinx.coroutines.flow.Flow

interface Searchable {}

interface SavableSearchable : Searchable, Comparable<SavableSearchable> {
    val key: String

    val label: String
    val labelOverride: String?
        get() = null
    fun overrideLabel(label: String): SavableSearchable

    /**
     * If this is true, tapping the item will open the details popup instead of launching it
     */
    val preferDetailsOverLaunch: Boolean


    val domain: String
    fun getSerializer(): SearchableSerializer

    override fun compareTo(other: SavableSearchable): Int {
        val label1 = labelOverride ?: label
        val label2 = other.labelOverride ?: other.label
        return  label1.compareTo(label2)
    }

    interface Companion {
        val domain: String
    }
}


interface SearchableSerializer {
    fun serialize(searchable: SavableSearchable): String?
    val typePrefix: String
}

class NullSerializer : SearchableSerializer{
    override fun serialize(searchable: SavableSearchable): String? {
        return null
    }

    override val typePrefix: String
        get() = "null"

}


interface SearchableDeserializer {
    suspend fun deserialize(serialized: String): SavableSearchable?
}

class NullDeserializer: SearchableDeserializer {
    override suspend fun deserialize(serialized: String): SavableSearchable? {
        return null
    }

}

interface SearchableRepository<T : Searchable> {
    fun search(query: String, allowNetwork: Boolean): Flow<List<T>>
}